/**
 * The Panel displays the previews of multimedia files in a sliding window. Allows delete through drag and drop
 * 
 *  @author  remo.lemma@lu.unisi.ch
 */

package ch.unisi.inf.projects.java.uom.gui.itemeditor.multimedia;

import javax.swing.*;
import java.util.List;
import java.awt.*;
import java.awt.event.*;

import ch.unisi.inf.projects.java.uom.elements.*;
import ch.unisi.inf.projects.java.uom.gui.commands.IconedCommand;
import ch.unisi.inf.projects.java.uom.gui.UOMImages;

public final class MultimediaSelectorPanel extends JPanel {

	
	private static final long serialVersionUID = -1L;
	
	private static final Dimension dim = new Dimension(90,70);
	
	private static final Dimension maxDim = new Dimension(110,85);
	
	private static final int tot = 140;
	
	private final List<Photo> photos;
	
	private int position;
	
	private final PhotoPanel photoPanel;
	
	private final int size;
	
	private static final JButton back = new JButton();
	
	private static final JButton forward = new JButton();
	
	{
		back.setAction(new IconedCommand("", "Slide to the left", UOMImages.ARROW_BACK.getFileName()) {
			private static final long serialVersionUID = -1L;
			public void actionPerformed (final ActionEvent e) { setUp(position - 1,false); }
		});
		forward.setAction(new IconedCommand("", "Slide to the right", UOMImages.ARROW_NEXT.getFileName())  {
			private static final long serialVersionUID = -1L;
			public void actionPerformed (final ActionEvent e) { setUp(position + 1,false); }
		});
	}
	
	public MultimediaSelectorPanel (final List<Photo> listOfPhotos, final int size, final PhotoPanel photoPanel)
	{
		super();
		this.photos = listOfPhotos;
		this.position = 0;
		this.size = size;
		
		this.setLayout(new FlowLayout());
		this.photoPanel = photoPanel;
		this.photoPanel.addPhotoPanelListener(new PhotoPanelListener() { 
			public void photoAdded (final PhotoPanel panel, final Photo photo) {
				setUp(position,true);
			}
			public void photoRemoved (final PhotoPanel panel, final Photo photo) { removePhoto(photo); }
			public void setPreview (final PhotoPanel panel, final Photo photo) { return; }
			public void newContentDisplayed (final PhotoPanel panel, final Photo photo) { return; }
		});
		setUp(0,false);
	}
	
	private void removePhoto (final Photo photo)
	{
		int i = 0;
		for (i = 0; i < photos.size(); i++) {
			if (photos.get(i).equals(photo))
				break;
		}
		if (photo != null && photos.contains(photo)) {
			photos.remove(photo);
			photoPanel.forceNotifyDelete(photo);
		}	
		if (position > 0)
			setUp(position - 1, false);
		else
			setUp(0, false);
		if (i < photos.size())
			photoPanel.setContent(photos.get(i));
		else if (i-1 >= 0)
			photoPanel.setContent(photos.get(i-1));
		else
			photoPanel.setContent(null);
	}
	
	private void setUp (final int position, final boolean slide)
	{
		this.position = position;
		this.removeAll();

		if (slide) {
			this.position = photos.size() - (size / tot);
			if (this.position < 0)
				this.position = 0;
		}
		
		this.add(back);
		back.setEnabled(false);
		this.add(back);
		back.setEnabled((position > 0));
		int i = 0;
		for (i = this.position; i < photos.size(); i++) {
			if ((i - this.position) >= (size / tot))
				break;
			final PhotoPanel pane = new PhotoPanel((int) dim.getWidth(), (int) dim.getHeight());
			pane.setMaxDim(dim);
			pane.enableDrag();
			pane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			pane.setAlignmentY(Component.TOP_ALIGNMENT);
			pane.setAlignmentX(Component.LEFT_ALIGNMENT);
			pane.setContent(photos.get(i));
			pane.enablePopup();
			pane.addMouseListener(new MouseAdapter() {
				public void mouseEntered (final MouseEvent ev) { pane.setMaxDim(maxDim); pane.paintComponent(pane.getGraphics()); pane.requestFocus(); }
				public void mouseExited (final MouseEvent ev) { pane.setMaxDim(dim); repaint(); }
				public void mousePressed (final MouseEvent ev) { photoPanel.setContent(pane.getContent()); }
			});
			pane.addPhotoPanelListener(new PhotoPanelListener() {
				public void photoAdded (final PhotoPanel panel, final Photo photo) { return; }
				public void photoRemoved (final PhotoPanel panel, final Photo photo) { removePhoto(photo); }
				public void setPreview (final PhotoPanel panel, final Photo photo) { photoPanel.fireSetPreview(photo); }
				public void newContentDisplayed (final PhotoPanel panel, final Photo photo) { return; }
			});
			this.add(Box.createHorizontalStrut(40));
			this.add(pane);
		}
		this.add(Box.createHorizontalStrut(40));
		this.add(forward);
		forward.setEnabled((photos.size() > i));
		this.revalidate();
		this.repaint();
	}	
}
